package com.ysd.lis.controller.self;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.eq.EqEquipmentImage;
import com.ysd.lis.entity.slef.SelfMachine;
import com.ysd.lis.request.FeeItemPageParam;
import com.ysd.lis.request.eq.DelEquipmentImageDto;
import com.ysd.lis.service.self.SelfMachineService;
import com.ysd.util.PropertySlash;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whx
 * @since 2025-05-12
 */
@RestController
@RequestMapping("/self/machine")
public class SelfMachineController extends BaseController {

    @Autowired
    SelfMachineService machineService;

    @Value("${equipmentImagePath}")
    private String equipmentImagePath;
    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @ApiOperation("获取自助机列表")
    @GetMapping("/findMachines")
    public Result findMachines(FeeItemPageParam feeItemPageParam) {
        return machineService.findMachines(feeItemPageParam);
    }

    @ApiOperation("获取自助机列表")
    @GetMapping("/findMachineList")
    public Result findMachineList(FeeItemPageParam feeItemPageParam) {
        return machineService.findMachineList(feeItemPageParam);
    }

    @ApiOperation("根据唯一号获取自助机列表")
    @GetMapping("/findMachineBySuid")
    public Result findMachineBySuid(String suid) {
        return machineService.findMachineBySuid(suid);
    }


    @ApiOperation("新增或编辑自助机")
    @PostMapping("/savaOrUpdateMachine")
    public Result savaOrUpdateMachine(@RequestBody SelfMachine machine) {
        return machineService.savaOrUpdateMachine(machine);
    }

    @ApiOperation("删除自助机")
    @PostMapping("/deleteMachine")
    public Result deleteMachine(@RequestBody List<SelfMachine> machineList) {
        return machineService.deleteMachine(machineList);
    }

    @ApiOperation("上传图片")
    @PostMapping("/uploadSelfImage")
    public Result uploadSelfImage(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String selfId = request.getParameter("selfId");
        if (file == null) {
            return Result.fail("文件不能为空") ;
        }

        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = equipmentImagePath + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + file.getOriginalFilename();
        String path = folder + fileName;
        File files1 = new File(path);
        if (FileUtil.exist(files1)) {
            return Result.fail("文件已存在");
        }
        File file2 = FileUtil.writeBytes(file.getBytes(), path);
        if (file2.length() < 0) {
            return Result.fail("文件上传失败");
        }
        Map<String, String> map = new HashMap<>();
        map.put("name", fileName);
        map.put("path", "/image/equipment/" + fileName);

        return machineService.uploadSelfImage(selfId, map);

    }

    @ApiOperation("删除图片")
    @GetMapping("/deleteFileById")
    public Result deleteFileById(DelEquipmentImageDto delEquipmentImageDto) {
        SelfMachine image = machineService.getById(delEquipmentImageDto.getId());
        File file = new File(equipmentImagePath + PropertySlash.getSlash() + image.getImageName());
        if (file.isFile()) {
            file.delete();
        }
        LambdaUpdateWrapper<SelfMachine>lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(SelfMachine::getImageName, null)
                .set(SelfMachine::getImagePath, null)
                .eq(SelfMachine::getId, delEquipmentImageDto.getId());
        machineService.update(lambdaUpdateWrapper);
        return Result.succ(1, "删除成功", null);
    }
}
